package com.example.a2032.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.entity.DeviceInfo;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DeviceInfoAdapter extends RecyclerView.Adapter {

    String[] titleArray;
    String[] contentArray;


    public DeviceInfoAdapter(String[] titleArray, String[] contentArray) {
        this.contentArray = contentArray;
        this.titleArray = titleArray;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SettingViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_deviceinfo, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SettingViewHolder settingViewHolder = (SettingViewHolder) holder;
        settingViewHolder.tvInfoTitle.setText(titleArray[position]);
        settingViewHolder.tvInfoContent.setText(contentArray[position]);

    }

    @Override
    public int getItemCount() {
        return titleArray.length;
    }

    static class SettingViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_info_title)
        TextView tvInfoTitle;
        @BindView(R.id.tv_info_content)
        TextView tvInfoContent;

        public SettingViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    onItemClickListener itemClickListener;

    public void setOnItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface onItemClickListener {
        public void onItemClick(int position);
    }
    public void updateMacAddress(String address){
        contentArray[2]=address;
        notifyDataSetChanged();
    }
    public void updateDeviceVersion(String version){
        contentArray[1]="V"+version;
        notifyDataSetChanged();
    }

    private static final String TAG = "DeviceInfoAdapter";
    public void updateDeviceInfo(DeviceInfo deviceInfo){
        String address=deviceInfo.getDeviceAddress();
        String deviceVersion=deviceInfo.getDeviceVersion();
        if(!TextUtils.isEmpty(address))    contentArray[2]=address;
        if(!TextUtils.isEmpty(deviceVersion))contentArray[1]="V"+deviceVersion;
        Log.i(TAG, "updateDeviceInfo: ");
        notifyDataSetChanged();
    }
}
